import pyautogui
import cv2
import os
import sys
import time
from ultralytics import YOLO
import win32gui
import 检测框模块
import 主程序
from moveFb模块 import movefb
from find_class_module import find_class_and_perform_action
from yolo2.find_and_perform_action_module import find_and_perform_action

# 定义全局变量
hwnd = None
model = None


def assign_tasks(task_id):
    global hwnd, model
    # 禁用 pyautogui 的安全保护机制
    pyautogui.FAILSAFE = False

    # 加载 YOLOv8n 模型并指定使用GPU
    model = YOLO(r"h:\yol\yolo\v11\runs\detect\train41\weights\best.pt").to('cuda')

    hwnd = 检测框模块.find_window()
    if hwnd is None:
        print("未找到标题中包含'梦幻西游'的窗口")
        return

    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    window_width = right - left
    window_height = bottom - top

    if hasattr(检测框模块, 'MODULE_LOADED') and 检测框模块.MODULE_LOADED:
        print("检测框模块已正确加载")
    else:
        print("检测框模块加载失败")
    print("分任务模块启动成功，正在分配任务")

    # 任务动作映射
    task_actions = {
        31: action_kill_monster,
        32: action_recruit,
        52: action_send_letter,
        42: action_chudao
    }

    if task_id in task_actions:
        action = task_actions[task_id]
        print(f"识别到任务 ID: {task_id}，准备执行对应任务")
        action()
    else:
        print(f"未知任务 ID: {task_id}，退出")
        sys.exit()


# 杀妖任务逻辑
def action_kill_monster():
    global hwnd, model
    print("启动杀妖任务")

    def action_a():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')
        time.sleep(1)

    find_class_and_perform_action(hwnd, model, 1, action_a)

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
           置信度=0.3, click_type='left')

    def action_a():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=0, offset_y=0,
               置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 151, action_a)

    def action2():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

    find_and_perform_action(hwnd, model, 5, action2)
    主程序.main()
    sys.exit()


# 招贤任务逻辑
def action_recruit():
    global hwnd, model
    print("启动招贤任务")

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
           offset_y=-12, 置信度=0.3, click_type='left')
    time.sleep(0.1)

    def action_b():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-100,
               offset_y=50, 置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 148, action_b)

    def action_c():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 1, action_c)

    def action_A():
        print("等待3秒截图")
        time.sleep(3)
        # 保存游戏窗口截图
        screenshot = 检测框模块.get_window_screenshot(hwnd)
        save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
        cv2.imwrite(save_path, screenshot)
        print(f"游戏窗口截图已保存到 {save_path}")

        def action1():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=40, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

        find_and_perform_action(hwnd, model, 57, action1)

        def action2():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=38, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

        find_and_perform_action(hwnd, model, 41, action2)

        def action3():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=39, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left',  success_image=151)

        find_and_perform_action(hwnd, model, 58, action3)

        def action4():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=37, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

        find_and_perform_action(hwnd, model, 59, action4)

        def action5():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=22, offset_x=0,
                   offset_y=-10, 置信度=0.3, click_type='left', success_image=151)

        find_and_perform_action(hwnd, model, 56, action5)

    pyautogui.hotkey('alt', 'e')
    pyautogui.hotkey('alt', 'e')
    find_class_and_perform_action(hwnd, model, 151, action_A)

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
           置信度=0.3, click_type='left')

    主程序.main()
    sys.exit()


# 送信任务逻辑
def action_send_letter():
    global hwnd, model
    print("启动送信任务")

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
           offset_y=-10, 置信度=0.3, click_type='left', mouse_shape_image=r'F:\zdh\yolo2\png\fb2.png')

    def action_b():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 70, action_b)

    def action_c():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=70, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 5, action_c)

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
           置信度=0.3, click_type='left')

    sys.exit()


# 除盗任务逻辑
def action_chudao():
    global hwnd, model
    def action_a():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 50, action_a)

    def action_b():
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=50, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

    find_class_and_perform_action(hwnd, model, 1, action_b)

    def action_c():
        print("秒过去")
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=60, offset_y=0,
               置信度=0.3, click_type='left')
        pyautogui.moveRel(-60, 0)
        screenshot = 检测框模块.get_window_screenshot(hwnd)
        save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
        cv2.imwrite(save_path, screenshot)
        print(f"游戏窗口截图已保存到 {save_path}")
        pyautogui.click()
        pyautogui.click()
        pyautogui.moveRel(60, 0)
        pyautogui.hotkey('alt', 'q')
        pyautogui.hotkey('alt', 'q')

    find_class_and_perform_action(hwnd, model, 151, action_c)

    movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
           置信度=0.3, click_type='left')

    主程序.main()
    sys.exit()
